package com.example.module_mine.login.viewmodel

import com.example.lib_common.base.BaseFragment
import com.example.lib_common.base.BaseViewModel
import com.example.lib_common.state.UIState
import com.example.lib_common.utils.MD5
import com.example.lib_storage.AuthMMKV
import com.example.module_mine.login.SmsCode
import com.example.module_mine.login.repository.LoginRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import okhttp3.RequestBody
import javax.inject.Inject

/**
 * @author yaotianxue
 * @description
 * @date 2025/2/5
 */
@HiltViewModel
class LoginViewModel @Inject constructor(private val repository: LoginRepository):BaseViewModel() {
    //请求发送验证码的时间
    var sendSmsCodeTimeStamp:Long = 0



    //当前正在显示的fragment
    private val mCurrentFragment = MutableStateFlow<String>("")
    val mCurrentFragment_ = mCurrentFragment.asStateFlow()
    //数据双向绑定手机号
    val phone:MutableStateFlow<String> = MutableStateFlow("")

    fun clearPhone(){
        phone.value = ""
    }

    /**
     * 设置正在显示的fragment
     */
    fun setCurrentFragment(fragment: String){
        mCurrentFragment.value = fragment
    }



    /**
     * 登陆/忘记密码发送验证码接口
     * type:login 登陆 reset 忘记
     */

    private val mRequestSmsCode = MutableStateFlow<UIState>(UIState.Default) //可变
    val mRequestSmsCode_ = mRequestSmsCode.asStateFlow() //不可变
    fun sendSmsCode(type:String){
        execute(
            action = if(type === SmsCode.LOGIN.type)  repository.sendLoginCode(phone.value) else repository.sendForgetPwdCode(phone.value),
            onStart = {
                mRequestSmsCode.value = UIState.Loading
            },
            onFinished = {
                mRequestSmsCode.value = UIState.Success
            },
            onError = {
                mRequestSmsCode.value = UIState.Error(it)
            }
        )
    }
    /**
     * 登陆验证码校验
     */
    private val mRequestLoginAuth = MutableStateFlow<UIState>(UIState.Default) //可变
    val mRequestLoginAuth_ = mRequestLoginAuth.asStateFlow() //不可变
    fun loginAuth(password:String){
        execute(
            action = repository.loginAuth(password,phone.value),
            onStart = {
                mRequestLoginAuth.value = UIState.Loading
            },
            onFinished = {
                mRequestLoginAuth.value = UIState.Success
                //保存token
                AuthMMKV.token = it.data.access_token
            },
            onError = {
                mRequestLoginAuth.value = UIState.Error(it)
            }
        )
    }
    /**
     * 账号密码登陆
     */
    fun login(password:String,userName:String){
        execute(
            action = repository.login(MD5.toMD5(password),userName),
            onStart = {
                mRequestLoginAuth.value = UIState.Loading
            },
            onFinished = {
                mRequestLoginAuth.value = UIState.Success
            },
            onError = {
                mRequestLoginAuth.value = UIState.Error(it)
            }
        )
    }

    /**
     * 忘记密码验证码校验
     */
    private val mRequestResetAuth = MutableStateFlow<UIState>(UIState.Default) //可变
    val mRequestResetAuth_ = mRequestResetAuth.asStateFlow() //不可变
    fun resetPwdAuth(smsCode:String,userName:String){
        execute(
            action = repository.resetPwdAuth(smsCode,userName),
            onStart = {
                mRequestResetAuth.value = UIState.Loading
            },
            onFinished = {
                mRequestResetAuth.value = UIState.Success
            },
            onError = {
                mRequestResetAuth.value = UIState.Error(it)
            }
        )
    }
    /**
     * 绑定密码
     * 加密：
     * 1.可逆:
     *  对称加密：加密解密用同一个密钥，效率高，安全差
     *  非对称加密：加密解密用两个密钥，效率低，安全高 （公钥/私钥）
     * 2.不可逆：MD5 密码发送服务器，服务器不能解开
     *
     * https采用非对称加密和对称加密均采用
     */
    private val mRequestResetPwd= MutableStateFlow<UIState>(UIState.Default) //可变
    val mRequestResetPwd_ = mRequestResetPwd.asStateFlow() //不可变
    fun resetPwd(smsCode:String,userName:String,password:String,newPassword:String){
        execute(
            action = repository.resetPwd(smsCode,userName, MD5.toMD5(password),MD5.toMD5(newPassword)),
            onStart = {
                mRequestResetPwd.value = UIState.Loading
            },
            onFinished = {
                mRequestResetPwd.value = UIState.Success
            },
            onError = {
                mRequestResetPwd.value = UIState.Error(it)
            }
        )
    }




}